package org.test.aop;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Hy on 2018/7/6.
 */
public class Client {
    public static void main(String[] args) {
        //1.编程方式
//        ProxyFactory proxyFactory=new ProxyFactory();//创建代理工厂
//        proxyFactory.setTarget(new GreetingImpl());
//        //方式1
////        proxyFactory.addAdvice(new GreetingBeforeAdvice());
////        proxyFactory.addAdvice(new GreetingAfterAdvice());
//        //方式2
////        proxyFactory.addAdvice(new GreetingBeforeAndAfterAdvice());
//        //方式3
//        proxyFactory.addAdvice(new GreetingAroundAdvice());
//        Greeting greeting = (Greeting) proxyFactory.getProxy();
//        greeting.sayHello("Jack");
        //2.通过配置文件方式
        //获取Spring Context
        ApplicationContext context = new ClassPathXmlApplicationContext("aspect.xml");
        //从Context中根据id获取Bean对象（其实就是一个代理）
            //Greeting greeting = (Greeting) context.getBean("greetingProxy");
            //greeting.sayHello("Tom");//调用代理方法
        GreetingImpl greetingImpl = (GreetingImpl) context.getBean("greetingImpl");
        greetingImpl.sayHello("Tom");//调用代理方法
        greetingImpl.goodMorning("Rose");

        Apology apology= (Apology) greetingImpl;
        apology.saySorry("Jack");

    }
}
